package cn.com.huzo.repository.trade.impl;

import cn.com.huzo.model.entities.trade.ProductionInstructionLinesColourOrderEntity;
import cn.com.huzo.model.entities.trade.ProductionInstructionLinesEntity;
import huzofw.repository.impl.GenericRepositoryImpl;
import huzofw.repository.impl.MyBatisDaoUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Repository;
import cn.com.huzo.repository.trade.ProductionInstructionLinesRepository;

/**
 * 生产指示书明细数据访问对象。
 *
 * @author 吴旭
 */
@Repository("ProductionInstructionLinesRepository")
public class ProductionInstructionLinesRepositoryImpl extends GenericRepositoryImpl<ProductionInstructionLinesEntity, String>
        implements ProductionInstructionLinesRepository {

    private final String namespace = "cn.com.huzo.repository.trade.ProductionInstructionLinesRepository";

    /**
     * 构造函数,设置the instance to ProductRepositoryImpl.class.
     */
    public ProductionInstructionLinesRepositoryImpl() {
        super(ProductionInstructionLinesEntity.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Map<String, Object> fetchTotalQuantityByHeaderId(Long headerId) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchTotalQuantityByHeaderId"), headerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<ProductionInstructionLinesEntity> fetchLinesByHeaderIdAndStyleNo(Long headerId, String styleNo) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);
        criterion.put("StyleNo", styleNo);

        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchLinesByHeaderIdAndStyleNo"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<ProductionInstructionLinesEntity> fetchLinesPrintData(Long headerId) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);

        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchLinesPrintData"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String insert(ProductionInstructionLinesEntity instance) {
        getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insert"), instance);
        // 返回保存后的记录ID
        return instance.getLinesId();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer updateLines(List<ProductionInstructionLinesEntity> itemList) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "update"), itemList);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer insertLinesByNewColour(Long headerId, Long colourId) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);
        criterion.put("ColourId", colourId);

        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insertLinesByNewColour"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer insertColourOrderLinesByNewColour(Long headerId) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("HeaderId", headerId);

        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insertColourOrderLinesByNewColour"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer updateLinesColour(List<ProductionInstructionLinesEntity> itemList) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "batchUpdateColour"), itemList);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer syncDeleteLinesColourOrder(Long headerId) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "syncDeleteLinesColourOrder"), headerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer updateLinesShippingMarkNo(List<ProductionInstructionLinesEntity> itemList) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "batchUpdateShippingMarkNo"), itemList);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer delete(String key) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "delete"), key);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Map<String, Object>> fetchProductColourFromMoByHeaderId(Long headerId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductColourFromMoByHeaderId"), headerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Map<String, Object>> fetchProductColourFromMoByProductId(Long productId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductColourFromMoByProductId"), productId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Map<String, Object>> fetchProductSizeByMoHeaderIdAndStyleNo(Long headerId, String styleNo) {
        Map<String, Object> criterion = new HashMap();
        criterion.put("MoHeaderId", headerId);
        criterion.put("StyleNo", styleNo);
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductSizeByMoHeaderIdAndStyleNo"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Map<String, Object>> fetchProductSizeFromMoByProductId(Long productId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductSizeFromMoByProductId"), productId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer fetchMaxRowNumPerColour(Long headerId) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchMaxRowNumPerColour"), headerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Map<String, Object> fetchProductQuantityByColour(Long headerId, Long colourId) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("HeaderId", headerId);
        criterion.put("ColourId", colourId);

        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchQuantityByColour"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Map<String, Object> fetchProductQuantityPerColour(Long moHeaderId, Long colourId) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("MoHeaderId", moHeaderId);
        criterion.put("ColourId", colourId);

        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductQuantityPerColour"), criterion);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer insertLinesColourOrder(List<Map<String, Object>> itemList) {
        return getSqlSession().insert(MyBatisDaoUtils.getMapperQueryId(
                namespace, "insertLinesColourOrder"), itemList);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer updateLinesColourOrder(List<Map<String, Object>> itemList) {
        return getSqlSession().update(MyBatisDaoUtils.getMapperQueryId(
                namespace, "updateLinesColourOrder"), itemList);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<ProductionInstructionLinesColourOrderEntity> fetchLinesColourOrderByHeaderId(Long headerId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchLinesColourOrderByHeaderId"), headerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Integer deleteLinesColourOrder(Long headerId) {
        return getSqlSession().delete(MyBatisDaoUtils.getMapperQueryId(
                namespace, "deleteLinesColourOrder"), headerId);
    }

    // <editor-fold defaultstate="collapsed" desc="物料需求相关用">
    /**
     * {@inheritDoc}
     */
    @Override
    public List<Map<String, Object>> fetchProductQuantityWithColour(Long headerId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductQuantityWithColour"), headerId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Map<String, Object> fetchProductInfoWithSize(Long headerId, Long productId) {
        Map<String, Object> criterion = new HashMap<>();
        criterion.put("MoHeaderId", headerId);
        criterion.put("ProductId", productId);

        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductInfoWithSize"), criterion);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="订单相关用">
    /**
     * {@inheritDoc}
     */
    @Override
    public Integer fetchMaxColourNumFromMoByPoHeaderId(Long poHeaderId) {
        return getSqlSession().selectOne(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchMaxColourNumFromMoByPoHeaderId"), poHeaderId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Map<String, Object>> fetchProductColourFromMoByPoHeaderId(Long poHeaderId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductColourFromMoByPoHeaderId"), poHeaderId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Map<String, Object>> fetchProductSizeFromMoByPoHeaderId(Long poHeaderId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductSizeFromMoByPoHeaderId"), poHeaderId);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<Map<String, Object>> fetchProductQuantityFromMoByPoHeaderId(Long poHeaderId) {
        return getSqlSession().selectList(MyBatisDaoUtils.getMapperQueryId(
                namespace, "fetchProductQuantityFromMoByPoHeaderId"), poHeaderId);
    }
    // </editor-fold>
}
